package com.top.mall.main.goods.adapter

import android.widget.CheckBox
import android.widget.ImageView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.module.lemlin.image.loadCenterImage
import com.module.lemlin.image.loadRoundImage
import com.top.mall.R
import com.top.mall.entity.CartList
import com.top.mall.entity.CateList
import com.top.mall.entity.GoodsCollect
import com.top.mall.entity.GoodsList
import com.top.mall.utils.SpanUtils
import java.lang.StringBuilder

class GoodsHomeAdapter : BaseQuickAdapter<GoodsList, BaseViewHolder>(R.layout.item_goods_home_body) {
    override fun convert(holder: BaseViewHolder, item: GoodsList) {
        holder.setText(R.id.text_goods_name,item.goods_name)
            .setText(R.id.text_goods_price,SpanUtils.setMoneySymbol(item.price, item.active_price))
            .getView<ImageView>(R.id.image_goods_icon)
            .loadCenterImage(item.image)
    }
}

class GoodsCategoryAdapter(private val type: Type) :
    BaseQuickAdapter<GoodsList, BaseViewHolder>(R.layout.item_goods_category_body) {

    enum class Type {
        CATEGORY, COLLECT
    }

    init {
        addChildClickViewIds(R.id.flCategoryBodyPlus, R.id.flCategoryBodyReduce)
    }

    override fun convert(holder: BaseViewHolder, item: GoodsList) {
        holder.setText(R.id.tvCategoryBodyName, item.goods_name)
            .setText(
                R.id.tvCategoryBodyPrice, SpanUtils.setMoneySymbol(item.price, item.active_price)
            )
            .setText(R.id.tvCategoryBodyNum, item.cart_num.toString())
            .setGone(R.id.llCategoryBodyReduceAndCount, (item.cart_num == 0))
            .setGone(R.id.divider, (type == Type.COLLECT))
        holder.getView<ImageView>(R.id.ivCategoryBodyPic)
            .loadRoundImage(item.image)
        holder.itemView.setBackgroundResource(
            if (type == Type.COLLECT) R.drawable.bg_radius_05dp_white else 0
        )
        holder.itemView.tag = holder.layoutPosition
    }


    fun setData(position: Int, goodsNum: Int, cartId: Int = 0) {
        this.setData(position, data[position].apply {
            cart_id = if (cartId == 0) cart_id else cartId
            cart_num = goodsNum
        })
    }
}

class GoodsCollectAdapter(private val type: Type = Type.COLLECT) :
    BaseQuickAdapter<GoodsCollect, BaseViewHolder>(R.layout.item_goods_category_body) {

    enum class Type {
        CATEGORY, COLLECT
    }

    init {
        addChildClickViewIds(R.id.flCategoryBodyPlus, R.id.flCategoryBodyReduce)
    }

    override fun convert(holder: BaseViewHolder, item: GoodsCollect) {
        holder.setText(R.id.tvCategoryBodyName, item.goods.goods_name)
            .setText(
                R.id.tvCategoryBodyPrice, SpanUtils.setMoneySymbol(item.goods.price, item.goods.price)
            )
            .setText(R.id.tvCategoryBodyNum, item.cart.goods_num.toString())
            .setGone(R.id.llCategoryBodyReduceAndCount, (item.cart.goods_num == 0))
            .setGone(R.id.divider, (type == Type.COLLECT))
        holder.getView<ImageView>(R.id.ivCategoryBodyPic)
            .loadRoundImage(item.goods.image)
        holder.itemView.setBackgroundResource(
            if (type == Type.COLLECT) R.drawable.bg_radius_05dp_white else 0
        )
        holder.itemView.tag = holder.layoutPosition
    }


    fun setData(position: Int, goodsNum: Int, cartId: Int = 0) {
        this.setData(position, data[position].apply {
            cart.id = if (cartId == 0) cart.id else cartId
            cart.goods_num = goodsNum
        })
    }
}

class GoodsLabelAdapter :
    BaseQuickAdapter<CateList, BaseViewHolder>(R.layout.item_goods_category_label) {

    var selectedPosition = 0
        set(value) {
            if (field != value) {
                field = value
                notifyDataSetChanged()
            }
        }

    override fun convert(holder: BaseViewHolder, item: CateList) {
        holder.setText(R.id.rvCategoryLabelName, item.name)
        holder.itemView.isSelected = holder.layoutPosition == selectedPosition
    }
}

class GoodsCartAdapter : BaseQuickAdapter<CartList, BaseViewHolder>(R.layout.item_goods_cart_body) {

    init {
        addChildClickViewIds(
            R.id.cbGoodsCartOpt,
            R.id.flGoodsCartReduce,
            R.id.flGoodsCartPlus,
            R.id.ivGoodsCartDelete
        )
        //view.animate().scaleX(1f).translationX(ConvertUtils.dp2px(35.5f).toFloat()).start()
        //view.animate().scaleX(0f).translationX(ConvertUtils.dp2px(35.5f).toFloat()).start()
    }

    override fun convert(holder: BaseViewHolder, item: CartList) {
        holder.setText(R.id.tvGoodsCartName, item.goods_name)
            .setText(R.id.tvGoodsCartCount, item.goods_num.toString())
            .setText(
                R.id.tvGoodsCartPrice, SpanUtils.setMoneySymbol(item.goods_price, item.active_price)
            )
            .setText(
                R.id.tvGoodsCartHint, StringBuilder("比加入时下降").append(item.reduce_price).append("元")
            )
            .setGone(R.id.tvGoodsCartHint, item.reduce_price.isNullOrEmpty())

        holder.getView<ImageView>(R.id.ivGoodsCartPic)
            .loadRoundImage(item.image)
        holder.getView<CheckBox>(R.id.cbGoodsCartOpt).isChecked = item.isSelected

    }
}
